package com.baizhi;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 *
 * @author Administrator
 */
public class Send {

    /**
    * 发送验证码
     * 参数：
     *   postData：拼接的数据
     *   postUrl： url
     *
     *   http://sms.106jiekou.com/utf8/sms.aspx?
     *   account=用户账号&
     *   password=接口密码&
     *   mobile=号码&
     *   content=您的订单编码：888888。如需帮助请联系客服。
    * */
    public static String SMS(String postData, String postUrl) {
        try {
            //发送POST请求
            URL url = new URL(postUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setUseCaches(false);
            conn.setDoOutput(true);

            conn.setRequestProperty("Content-Length", "" + postData.length());
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.write(postData);
            out.flush();
            out.close();

            //获取响应状态
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                System.out.println("connect failed!");
                return "";
            }
            //获取响应内容体
            String line, result = "";
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            while ((line = in.readLine()) != null) {
                result += line + "\n";
            }
            in.close();
            return result;
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
        return "";
    }

    public static void main(String[] args) {
        /**
         * 发送验证码
         * 参数：
         *   postData：数据
         *   postUrl： 地址
         *
         *   http://sms.106jiekou.com/utf8/sms.aspx?
         *   account=用户账号&
         *   password=接口密码&
         *   mobile=号码&
         *   content=您的订单编码：888888。如需帮助请联系客服。
         *
         *   http://sms.106jiekou.com/utf8/sms.aspx?account=用户账号&password=接口密码&mobile=号码&content=您的订单编码：888888。如需帮助请联系客服。
         * */
        String postUrl="http://sms.106jiekou.com/utf8/sms.aspx";
        String postData="account=zhangcn&password=123456&mobile=15210382535&content=您的验证密码是：888888，如需帮助请联系客服。";

        String sms = SMS(postData, postUrl);

        System.out.println(sms);
    }
}
